package com.lqian.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lqian.blog.dao.mapper.CommentMapper;
import com.lqian.blog.dao.pojo.Comment;
import com.lqian.blog.dao.pojo.SysUser;
import com.lqian.blog.service.CommentsService;
import com.lqian.blog.service.SysUserService;
import com.lqian.blog.util.UserThreadLocal;
import com.lqian.blog.vo.CommentVo;
import com.lqian.blog.vo.Result;
import com.lqian.blog.vo.UserVo;
import com.lqian.blog.vo.params.CommentParam;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Melody_LQ
 * @Date 2021/9/12 9:34
 * @Description 显示每一篇文章的评论列表
 * @Since version-1.0
 */
@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 真正的copy方法
     * @param comment
     * @return
     */
    public CommentVo copy(Comment comment){
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        //时间格式化
        commentVo.setCreateDate(new DateTime(comment.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        Long authorId = comment.getAuthorId();
        //根据comment得到评论者id 查询UserVo用来记录返回评论者的信息
        UserVo userVo = sysUserService.findUserVoById(authorId);
        //评论者的信息
        commentVo.setAuthor(userVo);
        //评论的评论（子评论）
        List<CommentVo> commentVoList = findCommentsByParentId(comment.getId());
        //commentVo中List<CommentVo> childrens
        commentVo.setChildrens(commentVoList);
        if (comment.getLevel() > 1) {
            Long toUid = comment.getToUid();
            //SELECT * FROM ms_sys_user WHERE id=?
            //给谁回复评论的记录信息
            UserVo toUserVo = sysUserService.findUserVoById(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //看是否有ParentId  parentId  =  id(comment表的)
        queryWrapper.eq(Comment::getParentId,id);
        //并且级别是2 2代表是子评论
        queryWrapper.eq(Comment::getLevel,2);
        List<Comment> comments = this.commentMapper.selectList(queryWrapper);
        return copyList(comments);
    }

    /**
     * 最终copyList返回CommentVo  List<CommentVo>
     * @param commentList
     * @return
     */
    //Preparing: SELECT id,content,create_date,article_id,author_id,parent_id,to_uid,level
    //FROM ms_comment WHERE (parent_id = ? AND level = ?)
    public List<CommentVo> copyList(List<Comment> commentList){
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    /**
     * 根据文章id查询评论列表
     * @param articleId
     * @return
     */
    @Override
    public Result commentsByArticleId(Long articleId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,articleId);
        // level为1 --> 评论
        // level >1 --> pararentId --> 找到该评论的父评论  回复
        queryWrapper.eq(Comment::getLevel,1);
        //查询Comment
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        //用CommentVo返回
        return Result.success(copyList(comments));
    }

    @Override
    public Result comment(CommentParam commentParam) {
        // 获取当前用户信息
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        this.commentMapper.insert(comment);
        return Result.success(null);
    }
}
